/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare","fu/app/comm/page/page",
	"scprit/followup/app/workBench/workBenchTreatment.servers.js",
	"scprit/followup/app/workBench/workBenchTreatment.ui.js"],
	function($,$bb,$page,$workBenchServers,$workBenchUi){
	var event = {};
	/**
	 * 点击"添加患者"按钮跳转到"添加患者"页面
	 */
	event.onAddPatientClick = function(){
		$bb.openPage("addPatientTreatment");
	};

	/**
	 * 点击今日门诊患者跳转
	 */
	event.onBabyClick = function(){
		var rows = $(this).data("value");
		var index = $(this).index();
		var params = {
			followQueueId: rows[index].followQueueId,
			ptSchemeId: rows[index].ptSchemeid,
			patientId: rows[index].patientId,
			ptTaskId: rows[index].taskId  // 任务实例id
		};
		var configParams = {
			followQueueId: rows[index].followQueueId
		};
		// 如果nav里存在不隐藏的专病菜单，则进入不隐藏的诊疗页面；否则，进去隐藏的诊疗页面
		if( $('#left-navbar [data-name=specialDisease]').length > 0 ){
			$bb.selectNavbar("specialDisease",params,JSON.stringify(configParams));
		}else{
			$bb.openPage("specialDiseaseHide",params,JSON.stringify(configParams));
		}
	}

	/**
	 * 点击全部患者卡片进入详情页
	 */
	var onBabyCardClick = function(){
		var followQueueId = $(this).attr("followQueueId");
		var ptSchemeId = $(this).attr("patientSchemeId");
		var patientId = $(this).attr("patientId");
		var params = {
			followQueueId: followQueueId,
			ptSchemeId: ptSchemeId,
			patientId: patientId,
		};
		var configParams = {
			followQueueId: followQueueId
		};
		// 如果nav里存在不隐藏的专病菜单，则进入不隐藏的诊疗页面；否则，进去隐藏的诊疗页面
		if( $('#left-navbar [data-name=specialDisease]').length > 0 ){
			$bb.selectNavbar("specialDisease",params,JSON.stringify(configParams));
		}else{
			$bb.openPage("specialDiseaseHide",params,JSON.stringify(configParams));
		}
	};

	/**
	 * 按科研编号、手机号、姓名搜索患者时，传参有所不同
	 */
	var searchKeywordAjax = function(params){
		var reg = /^1\d{10}$/;
		if(reg.test(params.fuzzyVar)){
			params = $.extend(params,{phoneNum:params.fuzzyVar});
		}else{
			delete params.phoneNum;
		}
		$workBenchServers.ajaxPatientsQueue(params,ajaxPatientsQueueCallBack);
	};
	/**
	 * 点击姓按钮进行搜索
	 */
	var onFirstNameClick = function(){
		var keyword = $(this).html();
		var data = $(this).data();
		var params = {
			"flag":data.flag,
			"rows":data.rows,
			"systemType":data.systemType,
			"page":1,
			"patientName":keyword
		}
		$workBenchServers.ajaxPatientsQueue(params,ajaxPatientsQueueCallBack);
	}
	/**
	 * 输入姓名、手机号、科研编号进行搜索
	 */
	var onKeywordClick = function(){
		var data = $(this).data();
		$(this).prev().val("");
		var keyword = $(this).prev().val();
		var params = {
			"flag":data.flag,
			"rows":data.rows,
			"systemType":data.systemType,
			"page":1,
			"fuzzyVar":keyword
		}
		searchKeywordAjax(params);	
	}
	var onKeywordInput = function(){
		var data = $(this).data();
		var keyword = $(this).val();
		var params = {
			"flag":data.flag,
			"rows":data.rows,
			"systemType":data.systemType,
			"page":1,
			"fuzzyVar":keyword
		}
		searchKeywordAjax(params);
	}
	/**
	 * 改变队列进行搜索
	 */
	var onQueueChange = function(){
		var data = $(this).data();
		var queueId = $(this).val();
		var params = {
			"flag":data.flag,
			"rows":data.rows,
			"systemType":data.systemType,
			"page":1,
			"queueId":queueId
		}
		$workBenchServers.ajaxPatientsQueue(params,ajaxPatientsQueueCallBack);
	}

	/**
	 * 请求全部患者成功后的回调函数
	 */
	var ajaxPatientsQueueCallBack = function(data,params){
		if(data.rows && data.rows.length > 0){
			var pagesAll = Math.ceil(data.total / params.rows);
			if(data.FirstName){
				if( $("#windowFirstName > button").length == 0){
					$workBenchUi.firstNameInit({
						id:"windowFirstName",
						list:data.FirstName,
						rows:params.rows
					});
				}
				/* 姓查询 */
				$("#windowFirstName button").unbind("click");
				$("#windowFirstName button").data(params);
				$("#windowFirstName button").click(onFirstNameClick);
			}
			$workBenchUi.babyListInit({
				id:"windowBabyList",
				list:data.rows,
				rows:params.rows
			});
			/* 翻页 */
			$page.turnInit({
				id:"windowBottomPage",
				pagesAll:pagesAll,
				page:params.page
			},$workBenchServers.ajaxPatientsQueue,params,ajaxPatientsQueueCallBack);
			/* 点击随访患者卡片进入详情页 */
			$("#windowBabyList .clickbabycard").unbind("click");
			$("#windowBabyList .clickbabycard").click(onBabyCardClick);
		}else{
			$("#windowBabyList").html('<div class="col-sm-12 col-md-12 babycard-null">暂无数据..</div>');
			$("#windowBottomPage").html("");
		}
		/* 姓名、科研编号、手机号码查询 */
		$("#windowKeyword span").unbind("click");
		$("#windowKeyword span").data(params);
		$("#windowKeyword span").click(onKeywordClick);
		$("#windowKeyword input").unbind("input propertychange");
		$("#windowKeyword input").data(params);
		$("#windowKeyword input").bind("input propertychange",onKeywordInput);
		/* 根据队列名称查询 */
		$("#selectQueue select").unbind("change");
		$("#selectQueue select").data(params);
		$("#selectQueue select").change(onQueueChange);	
	};
	event.ajaxPatientsQueueCallBack = function(data,params){
		ajaxPatientsQueueCallBack(data,params);
	}
	
	return event;
});
